//
//  TraveMianDetailViewController.m
//  课余时间
//
//  Created by zgr on 16/4/8.
//  Copyright © 2016年 张大少. All rights reserved.
//

#import "TraveMianDetailViewController.h"
#import "RequestManager.h"
#import "TraveMainModel.h"
#import "TraveMainDetailCell.h"
#import "StrategyViewController.h"
#import "RouteViewController.h"
@interface TraveMianDetailViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *modelArray;
@end

@implementation TraveMianDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _modelArray = [NSMutableArray array];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT - 64) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerClass:[TraveMainDetailCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
 //   NSLog(@"======%@",_desID);
    
    [self requestWithUrlString:[NSString stringWithFormat:@"http://chanyouji.com/api/destinations/%@.json",_desID]];
}
-(void)requestWithUrlString:(NSString *)urlString
{
    [[[RequestManager alloc]init] requestWithUrl:urlString requestType:RequestTypeGet parDic:nil finish:^(NSData *data) {
        NSArray *bigArray = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
      //  NSLog(@"%@",bigArray);
        for (NSDictionary *dataDic in bigArray) {
            TraveMainModel *model = [[TraveMainModel alloc]init];
            
            [model setValuesForKeysWithDictionary:dataDic];
            
            [_modelArray addObject:model];
        }
       // NSLog(@"%@",_modelArray);
        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView reloadData];
        });
    } error:^(NSError *error) {
        
    }];
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //NSLog(@"%ld",_modelArray.count);
    return _modelArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    TraveMainDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = _modelArray[indexPath.row];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.strategy.tag = (indexPath.row + 1) * 100 + 1;
    cell.route.tag =  (indexPath.row + 1) * 100 + 2;
    cell.sinotour.tag = (indexPath.row + 1) * 100 + 3;
    cell.special.tag = (indexPath.row + 1) * 100 + 4;
    [cell.strategy addTarget:self action:@selector(strategyAction:) forControlEvents:UIControlEventTouchUpInside];
     [cell.sinotour addTarget:self action:@selector(sinotourAction:) forControlEvents:UIControlEventTouchUpInside];
     [cell.route addTarget:self action:@selector(routeAction:) forControlEvents:UIControlEventTouchUpInside];
     [cell.special addTarget:self action:@selector(specialAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

-(void)strategyAction:(UIButton *)button
{
    TraveMainModel *model = _modelArray[(button.tag - 1) / 100 -1];
    
    StrategyViewController *stra = [[StrategyViewController alloc]init];
    stra.descID = model.traveMaimID;
    
    [self.navigationController pushViewController:stra animated:YES];
    
    
}
-(void)sinotourAction:(UIButton *)button
{
    NSLog( @"%ld",button.tag);
}
-(void)routeAction:(UIButton *)button
{
    TraveMainModel *model = _modelArray[(button.tag - 2) / 100 -1];
    RouteViewController *route = [[RouteViewController alloc]init];
    route.descID = model.traveMaimID;
    [self.navigationController pushViewController:route animated:YES];
}
-(void)specialAction:(UIButton *)button
{
    NSLog( @"%ld",button.tag);
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ((KWIDTH -10) * 540 / 900) + 50 + 10;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
